
<div class="wrapper wrapper-content page-heading animated fadeInRight">

    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">List</a></li>
                </ul>
                <div class="tab-content">
                   <div class="row animate fadeIn">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Zone List</i></h5>
            <div class="ibox-tools" id="btnss" style="display: none;">
                <button type="button" id="savecode" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <a id="cancel_adds" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a>
                <a id="done_btn" style="display: none;" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Finish</a>
            </div>
        </div>
        <div class="ibox-content">
           <div id="zonelist">
              <table class="table">
                <thead>
                  <tr>
                    <th>Zone Code</th>
                    <th>Zone Child</th>
                    <th>Enable Flag</th>
                    <th>Field Label</th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach($zones as $z): ?>
                      <tr>
                        <td><?php echo $z->zone_code; ?></td>
                        <td><?php echo $z->child_zone; ?></td>
                        <td><?php echo $z->enable_flag; ?></td>
                        <td><?php echo $z->field_label ?></td>
                        <td><button zonecode="<?php echo $z->zone_code; ?>" id="<?php echo $z->zone_code; ?>" class="btn btn-sm btn-info edit_zones " type="button">Edit</button></td>
                      </tr>
                  <?php endforeach; ?>
                </tbody>
              </table>
           </div>
           <div id="editform" style="display: none;">
             <form class="form-horizontal" id="agadasdw" method="post">
              <input type="hidden" name="zonecode32" id="zonecode2">
               <div class="form-group">
                    <label class="col-lg-2 control-label">Zone:</label>
                    <div class="col-lg-3">
                      <p class="form-control-static" id="zonecode"></p>
                    </div>
                    <label class="col-lg-2 control-label">Child Zone:</label>
                    <div class="col-lg-3">
                      <p class="form-control-static" id="childzone"></p>
                    </div>
                </div>
                <div class="form-group">
                  <label class="col-lg-2 control-label">Enable Flag:</label>
                    <div class="col-lg-3">
                      <input type="checkbox" name="enable_flag" class="i-checks" id="enable_flag">
                       <!-- <input checked type="checkbox" class="i-checks" > -->
                    </div>
                  <label class="col-lg-2 control-label">Field Label:</label>
                    <div class="col-lg-3">
                       <input type="text" name="fieldlabel32" class="form-control" id="fieldlabel">
                    </div>
                </div>
                <!-- <a id="cancel_adds" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a> -->
             </form>
             <form id="zone_value" style="">
             <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>Value Name</th>
                    <th>Description</th>
                    <th>Enable Flag</th>
                    <th>Parent Value</th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
                      <tr>
                        <input type="hidden" name="zonecode" id="zonecodev">
                        <td style="margin:0px;padding:0px;"><input class="form-control" type="text" name="value_name"></td>
                        <td style="margin:0px;padding:0px;"><input class="form-control" type="text" name="description"></td>
                        <td style="margin:0px;padding:4px;" align="center"><input class="i-checks" type="checkbox" name="enable_flag"></td>
                        <td style="margin:0px;padding:0px;"><p></p></td>
                        <td style="margin:0px;padding:0px;" align="center"><button style="width:100%;height: 30px;" title="Save" class="btn btn-primary btn-outline" id="savevalue" type="button"><i class="fa fa-check"></i>
                            </button></td>
                      </tr>
                </tbody>
                <tbody id="tbl_data">
                  
                </tbody>
              </table>
              </form>
           </div>
        </div>
</div>
<div class="modal inmodal fade in" id="myModal5" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">Add Child</h4>
            </div>
            <div class="modal-body">
                <form id="createchildren">
                  <table class="table">
                      <thead>
                        <tr>
                          <th>Value Name</th>
                          <th>Description</th>
                          <th>Enable Flag</th>
                          <th></th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr><input type="hidden" name="parentid" id="parentid">
                          <input type="hidden" name="zonecode" id="zonecodev2">
                          <td><input class="form-control" name="value_name" type="text"></td>
                          <td><input class="form-control" type="text" name="description"></td>
                          <td align="center"><input class="i-checks" type="checkbox" name="enable_flag"></td>
                          <td><button title="Save" class="btn btn-primary btn-outline" id="create_child" type="button"><i class="fa fa-check"></i>
                            </button></td>
                        </tr>
                      </tbody>
                      <tbody id="childrens">
                        
                      </tbody>
                  </table>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="modal inmodal fade in" id="myModal2" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">Edit</h4>
            </div>
            <div class="modal-body">
                <form id="editchildren">
                  <table class="table">
                      <thead>
                        <tr>
                          <th>Value Name</th>
                          <th>Description</th>
                          <th>Enable Flag</th>
                          <th></th>
                        </tr>
                      </thead>
                      <tbody>
                        <tr><input type="hidden" name="valueid" id="valueid">
                          <td><input class="form-control" name="value_name" type="text"></td>
                          <td><input class="form-control" type="text" name="description"></td>
                          <td align="center"><input class="i-checks" type="checkbox" name="enable_flag"></td>
                          <td><button title="Save" class="btn btn-primary btn-outline" id="edit_child" type="button"><i class="fa fa-check"></i>
                            </button></td>
                        </tr>
                      </tbody>
                  </table>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<div class="modal inmodal fade in" id="myModal1" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">Edit Child</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="editchildrenform">
                <input type="hidden" name="val_id">
                  <div class="form-group">
                    <label class="col-lg-3 control-label">Value Name:</label>
                      <div class="col-lg-5">
                        <input type="text" class="form-control" name="value_name">
                      </div>
                  </div>
                  <div class="form-group">
                    <label class="col-lg-3 control-label">Description:</label>
                      <div class="col-lg-5">
                        <input type="text" class="form-control" name="description">
                      </div>
                  </div>
                  <div class="form-group">
                    <label class="col-lg-3 control-label">Enable Flag:</label>
                      <div class="col-lg-5">
                        <input type="checkbox" class="i-checks" name="enable_flag">
                      </div>
                  </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" id="editchildsave" class="btn btn-primary">Save</button>
                <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->
</div>
<input type="hidden" id="count_add" value="0">
<input type="hidden" id="count_edit" value="0">
<input type="hidden" id="count_view" value="0">
<script>
    $(document).ready(function(){
        $(".chosen").chosen({width:"100%;"});
        var count_edit  = 0;
        var count_view  = 0;

        $("#zone_value").validate({
                   rules: {
                       value_name: {
                          required: true
                       }
                   }
              });
              $("#createchildren").validate({
                   rules: {
                       value_name: {
                          required: true
                       }
                   }
              });

              $('.edit_zones').click(function(){
                  $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/getzone'); ?>",
                        data: {'zonecode': $(this).attr('zonecode')},
                        dataType: 'json',
                        success: function(data){
                          if(data.length != 0){
                            $('#zonelist').slideUp();
                            $('#editform').slideDown();
                            $('#btnss').show();
                            $('#editform #zonecode').text(data[0].zone_code);
                            $('#editform #zonecode2').val(data[0].zone_code);
                            $('#zone_value #zonecodev').val(data[0].zone_code);
                            $('#createchildren #zonecodev2').val(data[0].child_zone);
                            $('#editform #childzone').text(data[0].child_zone);
                            $('#editform #enable_flag').attr('checked',(data[0].enable_flag == 'Y' ? true : false));
                            $('#editform #fieldlabel').val(data[0].field_label);
                            $('#editform #enable_flag').iCheck('update');
                            if(data[0].child_zone == null){
                              $('#zone_value').hide();
                            }else{
                              $('#zone_value').show();
                            }
                            getdata();
                          }else{

                          }
                          $(".edit_zones").removeAttr('disabled');
                        },
                        beforeSend: function(){
                           $(".edit_zones").attr('disabled',true);
                        }
                    });
              });

              $(document).on('click','#savecode',function(){
                console.log('asdasd');
                  $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/save'); ?>",
                        data: $('#agadasdw').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 0){
                            notifymsg('success','Edit Successful');
                          }else{
                            notifymsg('danger',data.msg);
                          }
                          $("#savecode").removeAttr('disabled');
                        },
                        beforeSend: function(){
                           $("#savecode").attr('disabled',true);
                        }
                    });
              });

              $('#editchildsave').click(function(){
                  $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/editchild'); ?>",
                        data: $('#editchildrenform').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 0){
                            notifymsg('success','Update Success!');
                            getdata();
                          }else{
                             notifymsg('danger',data.msg);
                          }
                          $("#editchildsave").removeAttr('disabled');
                        },
                        beforeSend: function(){
                           $("#editchildsave").attr('disabled',true);
                        }
                    });
              });

              $('#create_child').click(function(){
                  if($('#createchildren').valid() == true){
                    $.ajax({
                          type: "POST",
                          url: "<?php echo base_url($this->session->userdata('forajax').'/savechild'); ?>",
                          data: $('#createchildren').serialize(),
                          dataType: 'json',
                          success: function(data){
                            if(data.err == 0){
                              getdatachild($('#createchildren input[name=parentid]').val());
                            }else{

                            }
                            $("#create_child").removeAttr('disabled');
                          },
                          beforeSend: function(){
                            $("#create_child").attr('disabled',true);
                          }
                      });
                  }
              });

               $('#savevalue').click(function(e){
                e.preventDefault;
                if($("#zone_value").valid() == true){
                  $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/savezones'); ?>",
                        data: $('#zone_value').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 0){
                            getdata();
                          }else{

                          }
                          $("#savevalue").removeAttr('disabled');
                        },
                        beforeSend: function(){
                           $("#savevalue").attr('disabled',true);
                        }
                    });
                }
              });

              $("#cancel_adds").click(function(){
                $('#zonelist').slideDown();
                $('#editform').slideUp();
                $('#btnss').hide();
              });

               $("#addForm").validate({
                     rules: {
                         account_id: {
                             required: true
                         },
                         amount: {
                             required: true
                         },
                         payment_type: {
                          required: true
                         }
                     }
                });

            $('#cancel_add').click(function () {
                swal({
                            title: "Are you sure?",
                            text: "Your work will not be save",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "Leave",
                            cancelButtonText: "Stay",
                            closeOnConfirm: true,
                            closeOnCancel: true },
                        function (isConfirm) {
                            if (isConfirm) {
                              swal("success");
                                close_add(); active_search();
                                notifymsg("hide","hide");
                            } else {
                                swal("Cancelled");
                            }
                        });
              });
            });
        $(document).on('click','.editchild',function(){
            var id = $(this).closest('tr').children('input[name=zone_value_id]').val();
            var val_name = $(this).closest('tr').children('.value_name').text();
            var desc = $(this).closest('tr').children('.desc').text();
            var ena = $(this).closest('tr').children('input[name=enable_flag]').val();
            console.log(id + ' ' + val_name + ' ' + desc + ' ' + ena);
            $('#editchildrenform input[name=val_id]').val(id);
            $('#editchildrenform input[name=value_name]').val(val_name);
            $('#editchildrenform input[name=description]').val(desc);
            $('#editchildrenform input[name=enable_flag]').attr('checked',(ena == 'Y' ? true : false));
            $('#editchildrenform input[name=enable_flag]').iCheck('update');
            $('#myModal1').modal('show');
        });
        $(document).on('click','#add_child',function(){
            var form_id = $(this).closest('tr').children('input[name=zone_value_id]').val();
            $('#createchildren #parentid').val(form_id);
            $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax').'/getzone'); ?>",
              data: {'zonecode':  $('#createchildren #zonecodev2').val()},
              dataType: 'json',
              success: function(data){
                if(data.length != 0){
                     if(data[0].enable_flag == 'Y'){
                  getdatachild(form_id);
                  $('#myModal5').modal('show');
                }
                }
              }
          });
            getdatachild(form_id);
            //alert(form_id);
        });

        function getdata(){
          var tmp = '';
           $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax').'/getzoneval'); ?>",
              data: {'zonecode': $('#zone_value #zonecodev').val()},
              dataType: 'json',
              success: function(data){
                if(data.length != 0){
                  $.each(data, function(i, item) {
                    tmp += "<tr><input name='enable_flag' type='hidden' value='"+data[i].enable_flag+"'/><input name='zone_value_id' type='hidden' value='"+data[i].zone_value_id+"'/><td class='value_name'>"+data[i].zone_value_name+"</td><td class='desc'>"+data[i].description+"</td><td align='center'><div class='state icheckbox_square-green "+(data[i].enable_flag == 'Y' ? 'checked' : '')+"'></div></td><td></td><td align='center'><button type='button' style='margin-left:5px;margin-bottom:1px' class='btn btn-sm btn-outline btn-info btn-xs editchild' title='Edit' id='edit_child'><i class='fa fa-pencil-square-o fa-lg'></i></button><button type='button' style='margin-left:5px;margin-bottom:1px' class='btn btn-sm btn-outline btn-info btn-xs' title='Add Child' id='add_child'><i class='fa fa-users fa-lg'></i></button></td></tr>";
                  });
                }else{
                }
                $('#tbl_data').html(tmp);
                $(".edit_zones").removeAttr('disabled');
              },
              beforeSend: function(){
                 $(".edit_zones").attr('disabled',true);
              }
          });
        }

        function getdatachild(id){
          var tmp = '';
           $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax').'/getzonechild'); ?>",
              data: {'parentid': id},
              dataType: 'json',
              success: function(data){
                if(data.length != 0){

                  $.each(data, function(i, item) {
                    tmp += "<tr><input name='zone_value_id' value='"+data[i].zone_value_id+"'/><td>"+data[i].zone_value_name+"</td><td>"+data[i].description+"</td><td align='center'><div class='state icheckbox_square-green "+(data[i].enable_flag == 'Y' ? 'checked' : '')+"'></div></td><td></td></tr>";
                  });

                }else{

                }

                $('#childrens').html(tmp);
                $(".edit_zones").removeAttr('disabled');
              },
              beforeSend: function(){
                 $(".edit_zones").attr('disabled',true);
              }
          });
        }
        $('#adds').click(function(){
            $('#notify').text("");
                if($('#count_add').val() == 0){
                    $('#tab-list-2').append('<li id="add-link" class="links"><a id="2nd" data-toggle="tab" href="#add">Add<button id="addX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                    $('.tab-content').append('<div id="add" class="tab-pane"><div class="panel-body"><div id="change2"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                    $("#change2").load("<?php echo $this->session->userdata('forload') . '/add_form'; ?>")
                    $('#count_add').val('1');
                    add_action('show'); search_action('close');
                }
        });
        $('.close-link').click(function(){
            $('#searchResult').hide();
        });

        $(document).on('click','.open_modal',function(){
            var form_id = $(this).closest('tr').children('input[name=form_id]').val();
            var email_dtl_id = $(this).closest('tr').children('input[name=email_dtl_id]').val();
            var times = $(this).closest('tr').children('input[name=times]').val();
            var lookup_code = $(this).closest('tr').children('input[name=lookup_code]').val();
            var ena_flag = $(this).closest('tr').children('input[name=enable_flag]').val();
            $('#report_modal input[name=email_dtl_id]').val(email_dtl_id);
            $('#report_modal select[name=report_id]').val(form_id);
            $('#report_modal select[name=times]').val(times);
            $('#report_modal select[name=schedule]').val(lookup_code);
            //$('#report_modal input[name=enable_flag]').attr('checked', (ena_flag == 'Y' ? true : false));
            $('#report_modal input[name=enable_flag]').iCheck((ena_flag == 'Y' ? 'check' : 'uncheck'));
            $(".chosen").trigger('chosen:updated');
            console.log(email_dtl_id);
            console.log(lookup_code);
            console.log(times);
            console.log(ena_flag);
         });

        $('#save_report_dtl_upd').click(function(){
            $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/updateReportDtl'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#report_modal #form_report_dtl_upd').serialize(),
                success: function(e) {
                  console.log(e);
                },
                beforeSend: function(){
                  $("#save_report_dtl_upd-btn").attr('disabled',true);
                }
              });
        });

        $("#search-btn").click(function(e) {
            e.preventDefault();
            $('#resultfound').empty();
            $('#searchResult').show();
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/get_status'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#searchForm').serialize(),
                success: function(e) {
                    if(e.result == null){
                        $('#resultfound').html("<td colspan='3' align='center'><h3>No Result Found</h3></td>");
                        notifymsg("danger","No Result Found");
                    }else{
                        $('#resultfound').html(e.result);
                        notifymsg("success","   . . . Search Success  , Found("+e.count+") Item(s)");
                    }
                },
                error: function() {
                },
                complete: function() {
                  $("#search-btn").removeAttr('disabled');
                },
                beforeSend: function(){
                  $("#search-btn").attr('disabled',true);
                }
              });
            });

        $(document).on('click','.edit-tab',function(){
            var moduleID = $(this).closest('tr').children('#boang').val();
            if($('#count_edit').val() == 0){
                $('#tab-list-2').append('<li id="edit-link" class="links"><a data-toggle="tab" href="#edit">View<button id="editX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="edit" class="tab-pane"><div class="panel-body"><div id="change3"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                $("#count_edit").val('1');
                }
            $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+moduleID);
            edit_action('show'); search_action('close');
            notifymsg("hide","hide");
            });
        $(document).on('click','.btn.btn-sm.btn-outline.btn-info.btn-xs.view',function(){
            var moduleID = $(this).closest('tr').children('#moduleId').val();
            if($('#count_view').val() == 0){
                $('#tab-list-2').append('<li id="view-link" class="links"><a data-toggle="tab" href="#view">View<button id="viewX" style="margin-left:30px;" class="close" type="button" title="Remove this page">×</button></a></li>');
                $('.tab-content').append('<div id="view" class="tab-pane"><div class="panel-body"><div id="change4"><div class="sk-spinner sk-spinner-double-bounce"><div class="sk-double-bounce1"></div><div class="sk-double-bounce2"></div></div></div></div></div>');
                $("#count_view").val('1');
                }
            $("#change4").load("<?php echo $this->session->userdata('forload') . '/view_form/'; ?>"+moduleID);
            view_action('show'); search_action('close');
            notifymsg("hide","hide");
            });
</script>